home *** CD-ROM | disk | FTP | other *** search
- /* COPYRIGHT NOTICE :
- * Copyright 1986 Eric Jul. May not be used for any
- * purpose without written permission from the author.
- */
-
- /* File: snapshot.c Eric Jul, 1986-05-03 */
-
- /*
- * $Header$
- * INTERFACE: See usage printout.
- *
- * FUNCTION: sends snapshot requests to the kernel
- *
- * IMPORTS: loads, see #include
- *
- * EXPORTS: Nothing.
- *
- * DESIGN: Send snapshot message to process and then
- * repeatedly receive and print data until stop snapshot
- * message arrives.
- *
- * $Log$
- * 1983-06-08 Initial implementation, Eric Jul.
- * 1985-03-04 Eric Jul
- * 1986-05-03 Modified to work with 4.2bsd sockets.
- */
-
- #undef integer
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
-
- #define mSUCCESS(X) (((X) & 7) == 1)
-
- typedef int KKStatus;
-
- #define MAXMESSAGESIZE 1000
- #include "Kernel/h/emPorts.h"
- #include "Kernel/h/kmdDefs.h"
-
- #include <netdb.h>
- #include <signal.h>
-
- #define endcase break
-
- char *snapshotname, *snapshotstring;
- int talkSock;
- int pid = 0;
- int prodTime = 1;
- int MType, MInt;
- char MString[5000];
- struct sockaddr_in destSockDescr = {AF_INET};
-
- /* Define signal handlers */
-
- void PipeHandler(sig)
- int sig;
- {
- printf("SIGPIPE>>> The connection to the process broke.\nTerminating.\n");
- exit(sig);
- }
-
- void TerminationHandler(sig)
- int sig;
- {
-
- if (sig != 0) {
- printf("Terminating snapshot due to signal %d.\n", sig);
- } else {
- printf("Snapshot done.\n");
- }
-
- /* Important to tell kernel that we are bye, bye ... */
- KMDSend(talkSock, KMDCLASTMSG, 0, "Done");
- if (shutdown(talkSock, 1) < 0) {
- perror("shutdown1");
- close(talkSock);
- exit(sig);
- }
-
- while(mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString)));
- close(talkSock);
- exit(sig);
- };
-
-
- void Prodder()
- /* Prods the kernel by sending a SIGURG signal.
- Exponential backoff.
- */
- {
- /* kill(pid, SIGURG); */
- prodTime += prodTime;
- alarm(prodTime);
- }
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int OK;
- char snapbuf[500];
- static int i, EPLNN, SValue;
- static int pidGiven = 0, machineNameGiven = 0;
- KKStatus kstat;
- char myHostName[100];
- struct hostent *myHost;
-
- snapshotname = "Menu"; /* default */
- snapshotstring = (char *) 0;
- SValue = 0; /* default */
- EPLNN = 8; /* default */
-
- if (argc == 2)
- if (!strcmp(argv[1], "usage")) {
- printf("Usage: snapshot [-m <machine name>] [-N <LNN> | -X <hexLNN> | -P <pid>] [-n <snapname>] [-{d|h} <parameter value> | -S <string>\n");
- exit(0);
- };
-
- /*
- * Parse command line options
- */
- while (argc > 1 && argv[1][0] == '-') {
- switch (argv[1][1]) {
- case 'N': /* Set logical node number */
- if (argv[1][2] == '\0') {
- EPLNN = atoi(argv[2]);
- argc--;
- argv++;
- } else
- EPLNN = atoi(&argv[1][2]);
-
- printf("warning: -N ignored.\n");
- break;
- case 'X': /* Set logical node number (hex) */
- if (argv[1][2] == '\0') {
- sscanf(argv[2], "%x", &EPLNN);
- argc--;
- argv++;
- } else
- sscanf(&argv[1][2], "%x", &EPLNN);
- printf("warning: -X ignored.\n");
- break;
- case 'D':
- KMDTest = 1;
- break;
- case 'd':
- if (argv[1][2] == '\0') {
- SValue = atoi(argv[2]);
- argc--;
- argv++;
- } else
- SValue = atoi(&argv[1][2]);
- break;
- case 'P': /* Set pid, cancel LNN usage. */
- if (argv[1][2] == '\0') {
- pid = atoi(argv[2]);
- argc--;
- argv++;
- } else
- pid = atoi(&argv[1][2]);
- pidGiven = 1;
- printf("warning: -P ignored.\n");
- break;
- case 'h':
- if (argv[1][2] == '\0') {
- sscanf(argv[2], "%x", &SValue);
- argc--;
- argv++;
- } else
- sscanf(&argv[1][2], "%x", &SValue);
- break;
- case 'n':
- snapshotname = argv[2];
- argc--;
- argv++;
- break;
- case 'S':
- snapshotstring = argv[2];
- argc--;
- argv++;
- break;
- case 'M':
- case 'm':
- strcpy(myHostName, argv[2]);
- argc--;
- argv++;
- machineNameGiven ++;
- break;
- default:
- printf("Warning: Unknown option: %c", argv[1][2]);
- break;
-
- }
- argc--;
- argv++;
- }
-
-
- /* Set termination handler. */
- signal(SIGHUP, TerminationHandler);
- signal(SIGQUIT, TerminationHandler);
- signal(SIGINT, TerminationHandler);
- signal(SIGBUS, TerminationHandler);
- signal(SIGSEGV, TerminationHandler);
- signal(SIGTERM, TerminationHandler);
- signal(SIGPIPE, PipeHandler);
- signal(SIGALRM, Prodder);
-
- talkSock = socket(AF_INET, SOCK_STREAM, 0, 0);
- if (talkSock < 0) {
- perror("snapshot: socket");
- exit(1);
- }
-
- if (! machineNameGiven) {
- if (gethostname(myHostName, 100) < 0) {
- perror("snapshot: gethostname");
- exit(1);
- }
- }
- myHost = gethostbyname(myHostName);
- if (myHost == NULL) {
- perror("snapshot: gethostbyname");
- exit(1);
- }
-
- bcopy(myHost->h_addr, &destSockDescr.sin_addr, myHost->h_length);
-
- {
- char *emplanename;
- int emPlane = 0;
- int emPort = EMKERNELDEFAULTPORTNUMBER;
- struct servent *myService;
- emplanename = (char *) getenv("EMPLANE");
- if (emplanename != NULL) emPlane = atoi(emplanename);
- #ifdef DIKU
- myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
- if(myService)
- emPort = ntohs(myService->s_port);
- #endif
- destSockDescr.sin_port = htons(emPort + emPlane*4 + 1);
- }
-
- if (connect(talkSock, &destSockDescr, sizeof(destSockDescr)) == -1) {
- perror("snapshot: connect");
- exit(1);
- }
-
-
- if ( KMDTest )
- printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
-
- KMDReceive(talkSock, &MType, &MInt, MString);
- printf("%s", MString);
-
- /* Start snapshot.*/
- sprintf(snapbuf, "%s %s", snapshotname, snapshotstring);
- kstat = KMDSend(talkSock, KMDCSNAPSHOT, SValue, snapbuf);
-
- if (! mSUCCESS( kstat ) ) {
- printf("snapshot: Send start snapshot msg failure, kstat = 0x%08x\n",
- kstat);
- exit(1);
- };
-
- printf("Calling %s on %s, param: %d (0x%02x)\n",
- snapshotname, myHostName, SValue, SValue);
-
- fflush(stdout);
- OK = 1;
- while (OK==1) {
- if(!mSUCCESS(kstat = KMDReceive(talkSock, &MType, &MInt, MString))){
- printf("Premature snapshot termination\n");
- perror("recv");
- printf("Kernel status 0x%06x\n");
- TerminationHandler(0);
- };
-
- if ( KMDTest ){
- printf("Msg from: %d, (%d, %d, %s)\n", talkSock,
- MType, MInt, MString),
- fflush(stdout);
- };
- switch ( MType ) {
-
- case KMDCDATA:
- printf("%s", MString);
- fflush(stdout);
- endcase;
-
- case KMDCERRMSG: /* Error message */
- printf("snapshot: %s\n", MString);
- fflush(stdout);
- OK = 0; /* quit */
- endcase;
-
- case KMDCLASTMSG:
- printf("%s", MString);
- OK = 0; /* quit. */
- endcase;
-
- default:
- printf("snapshot: Warning, bad msgtype: %d - ignored.\n", MType);
- };
- }
- TerminationHandler(0);
- }
-
- /* C O P Y R I G H T N O T I C E : */
- /* Copyright 1986 Eric Jul. May not be used for any */
- /* purpose without written permission from the author. */
-